package com.b2b.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.b2b.exception.CustomException;
import com.b2b.exception.CustomUnauthorizedException;
import com.b2b.model.PlatformToBepaidDto;
import com.b2b.model.common.BaseDto;
import com.b2b.model.common.ResponseBean;
import com.b2b.service.PlatformToBepaidService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/platformToBepaid")
@PropertySource("classpath:config.properties")
public class PlatformToBepaidController {

    @Value("${refreshTokenExpireTime}")
    private String refreshTokenExpireTime;

    private final PlatformToBepaidService platformToBepaidService;

    @Autowired
    public PlatformToBepaidController(PlatformToBepaidService platformToBepaidService){
        this.platformToBepaidService = platformToBepaidService;
    }


    @PostMapping
//    @RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean add(@RequestBody PlatformToBepaidDto platformToBepaidDto) {
        int count = platformToBepaidService.insert(platformToBepaidDto);
        if (count <= 0) {
            throw new CustomException("新增失败(Insert Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "新增成功(Insert Success)", platformToBepaidDto);
    }
    @GetMapping
    //@RequiresPermissions(logical = Logical.AND, value = {"user:view"})
    public ResponseBean findAll(@Validated @RequestBody BaseDto baseDto) {
        if (baseDto.getPage() == null || baseDto.getRows() == null) {
            baseDto.setPage(1);
            baseDto.setRows(10);
        }
        PageHelper.startPage(baseDto.getPage(), baseDto.getRows());
        List<PlatformToBepaidDto> platformToBepaidDtos = platformToBepaidService.selectAll();
        PageInfo<PlatformToBepaidDto> selectPage = new PageInfo<PlatformToBepaidDto>(platformToBepaidDtos);
        if (platformToBepaidDtos == null) {
            throw new CustomException("查询失败(Query Failure)");
        }
        Map<String, Object> result = new HashMap<String, Object>(16);
        result.put("count", selectPage.getTotal());
        result.put("data", selectPage.getList());
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", result);
    }

    @GetMapping("/byId")
    //@RequiresPermissions(logical = Logical.AND, value = {"user:view"})
    public ResponseBean findById(@RequestBody PlatformToBepaidDto platformToBepaidDto) {
        platformToBepaidDto = platformToBepaidService.selectByPrimaryKey(platformToBepaidDto.getBepaidId());
        if (platformToBepaidDto == null) {
            throw new CustomException("查询失败(Query Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "查询成功(Query was successful)", platformToBepaidDto);
    }

    @PutMapping
    //@RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean update( @RequestBody PlatformToBepaidDto platformToBepaidDto) {
        PlatformToBepaidDto platformToBepaidDtoTemp = new PlatformToBepaidDto();
        platformToBepaidDtoTemp.setBepaidId(platformToBepaidDto.getBepaidId());
        platformToBepaidDtoTemp = platformToBepaidService.selectOne(platformToBepaidDtoTemp);
        if (platformToBepaidDtoTemp == null) {
            throw new CustomUnauthorizedException("该帐号不存在(Account not exist.)");
        }
        int count = platformToBepaidService.updateByPrimaryKeySelective(platformToBepaidDto);

        if (count <= 0) {
            throw new CustomException("更新失败(Update Failure)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "更新成功(Update Success)", platformToBepaidDto);
    }

    @DeleteMapping
    // @RequiresPermissions(logical = Logical.AND, value = {"user:edit"})
    public ResponseBean delete(@RequestBody PlatformToBepaidDto platformToBepaidDto) {
        Integer id = platformToBepaidDto.getBepaidId();
        int count = platformToBepaidService.deleteByPrimaryKey(id);
        if (count <= 0) {
            throw new CustomException("删除失败，ID不存在(Deletion Failed. ID does not exist.)");
        }
        return new ResponseBean(HttpStatus.OK.value(), "删除成功(Delete Success)", null);
    }
}
